﻿#region Copyright (c) 2013-03, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_SubsystemFactoryExtensions
	{
		private class StubSubsystem : Subsystem
		{
			public StubSubsystem(RuntimeEnvironment environment)
				: base(environment)
			{
			}
		}

		private class StubEnvironment : RuntimeEnvironment
		{
			public StubEnvironment(IRuntime runtime, IMessageBus messageBus)
				: base(runtime, messageBus)
			{
			}
		}

		private class MockFactory : ISubsystemFactory
		{
			public Int32 Called;
			public Type TypeArgument;
			public RuntimeEnvironment EnvironmentArgument;
			public StubSubsystem ReturnValue;

			public ISubsystem LoadSubsystem(Type subsystemType, RuntimeEnvironment environment)
			{
				TypeArgument = subsystemType;
				EnvironmentArgument = environment;
				Called++;

				ReturnValue = new StubSubsystem(environment);
				return ReturnValue;
			}
		}


		// --- TESTS ---


		#region LoadSubsystem<T>()

		[Test]
		public void Test_LoadSubsystem_GenericType_Environment()
		{
			var runtime = new DummyRuntime();
			var messageBus = new DummyMessageBus();
			var environment = new RuntimeEnvironment(runtime, messageBus);
			var factory = new MockFactory();

			var subsystem = factory.LoadSubsystem<StubSubsystem>(
				environment);

			Assert.That(factory.Called, Is.EqualTo(1));
			Assert.That(factory.TypeArgument, Is.EqualTo(typeof(StubSubsystem)));
			Assert.That(factory.EnvironmentArgument, Is.SameAs(environment));
			Assert.That(subsystem, Is.SameAs(factory.ReturnValue));
		}

		#endregion

		#region LoadSubsystem<T, E>()

		[Test]
		public void Test_LoadSubsystem_GenericType_GenericEnvironment()
		{
			var runtime = new DummyRuntime();
			var messageBus = new DummyMessageBus();
			var environment = new StubEnvironment(runtime, messageBus);
			var factory = new MockFactory();

			var subsystem = factory.LoadSubsystem<StubSubsystem, StubEnvironment>(
				environment);

			Assert.That(factory.Called, Is.EqualTo(1));
			Assert.That(factory.TypeArgument, Is.EqualTo(typeof(StubSubsystem)));
			Assert.That(factory.EnvironmentArgument, Is.SameAs(environment));
			Assert.That(subsystem, Is.SameAs(factory.ReturnValue));
		}

		#endregion

	}
}

#endif
